package com.hopes.bizz.protocol.hj212.activemq.producer;

import com.hopes.bizz.protocol.hj212.activemq.config.ActiveMqStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/5/25 13:55
 * @Description:
 */
@Component
@Slf4j
public class ActiveMqTopicSender {
    @Autowired
    private ActiveMqStore activeMqStore;

    public void send(String topic, String message){
        try {
            Session session = activeMqStore.getSession();
            MessageProducer producer = session.createProducer(session.createTopic(topic));
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            TextMessage msg = session.createTextMessage(message);
            producer.send(msg);
            producer.close();
        } catch (JMSException e) {
            log.error("消息发送失败,错误码={},原因={}", e.getErrorCode(), e.getMessage());
        }
    }
}
